package com.jihox.abookpro.view;

import java.util.ArrayList;
import java.util.List;

import com.jihox.abookpro.domain.Sentence;
import com.jihox.abookpro.util.DensityUtil;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.Animator.AnimatorListener;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

public class VerticalScrollTextView extends TextView {

	private float mX;
	private Paint mPathPaint;	
	public int index = 0;
	private List<Sentence> list;
	public float mTouchHistoryY;
	private float middleY;
	private Context context;
	private boolean stopTextView =false;
	
	public VerticalScrollTextView(Context context) {
		super(context);
		this.context = context;
		init();
	}
	public VerticalScrollTextView(Context context, AttributeSet attr) {
		super(context, attr);
		this.context = context;
		init();
	}
	public VerticalScrollTextView(Context context, AttributeSet attr, int i) {
		super(context, attr, i);
		this.context = context;
		init();
	}
	private void init() {
		setFocusable(true);
		if(list==null){
			list=new ArrayList<Sentence>();
			Sentence sen=new Sentence(0,"");
			list.add(0, sen);
		}		
		
		mPathPaint = new Paint();
		mPathPaint.setAntiAlias(true);
		mPathPaint.setColor(Color.WHITE);
		mPathPaint.setTextSize(DensityUtil.dip2px(context, 15f));
		mPathPaint.setTypeface(Typeface.SANS_SERIF);
		mPathPaint.setTextAlign(Paint.Align.CENTER);
	}
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (index == -1)
			return;
		canvas.drawText(list.get(index).getName(), mX, middleY, mPathPaint);
	}
	protected void onSizeChanged(int w, int h, int ow, int oh) {
		super.onSizeChanged(w, h, ow, oh);
		mX = w * 0.5f; 
		middleY = h * 0.5f;
	}

	public long updateIndex(int index) {	
		if (index == -1)
			return -1;
		this.index=index;		
		return index;
	}
	
	public List<Sentence> getList() {
		return list;
	}
	
	public void setList(List<Sentence> list) {
		this.list = list;
	}
	public void updateUI(boolean stopTextView){
		this.stopTextView = stopTextView;
		new Thread(new updateThread()).start();
	}
	class updateThread implements Runnable {
		long time = 5000; 
		int i=0;
		public void run() {
			while (!stopTextView) {
				long sleeptime = updateIndex(i);
//				time += sleeptime;
				mHandler.post(mUpdateResults);
				if (sleeptime == -1)
					return;
				try {
					Thread.sleep(time);
					i++;
					if(i==getList().size())
						i=0;
				} catch (InterruptedException e) {					
					e.printStackTrace();
				}
			}
			
		}
	}
	Handler mHandler = new Handler();
	Runnable mUpdateResults = new Runnable() {
		public void run() {
			addAnimationForScrollTextView(VerticalScrollTextView.this);
			invalidate();
		}
	};
	private void addAnimationForScrollTextView(final TextView textView){
		ObjectAnimator fadeIn = ObjectAnimator.ofFloat(textView, "alpha", 0f,0.5f,0.8f,1f,1f,1f);
		ObjectAnimator fadeOut = ObjectAnimator.ofFloat(textView, "alpha",1f,1f,1f,0.8f,0.5f,0f);
		
		fadeIn.setDuration(2500);
		fadeOut.setDuration(2500);
		AnimatorSet set0 = new AnimatorSet();
		set0.play(fadeIn).before(fadeOut);
		set0.addListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator arg0) {
			}
			@Override
			public void onAnimationRepeat(Animator arg0) {
			}

			@Override
			public void onAnimationEnd(Animator arg0) {
			}

			@Override
			public void onAnimationCancel(Animator arg0) {
			}
		});
		set0.start();
	}
}
